package com.lance.shiro.service;

import com.lance.shiro.entity.IUser;
import com.lance.shiro.mapper.PropertyMapper;
import com.lance.shiro.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
@Transactional
public class StatisticsService {
    @Autowired
    private PropertyMapper propertyMapper;
    @Autowired
    private UserMapper userMapper;

    public Map<String, String> findAgentPropertyManagement(String user) {
        return propertyMapper.findAgentPropertyManagement(user);
    }

    public Map<String, String> findAgentSalesyRecord(String user) {
        return propertyMapper.findAgentSalesyRecord(user);
    }

    public List<Map<String, Object>> findAgentProperty(String referid) {
        List<IUser> userList = userMapper.findAgentByReferId(referid);
        List<Map<String, Object>> mapList = new ArrayList<>();
        if (null != userList && userList.size() > 0) {
            for (IUser iUser : userList) {
                Map<String, Object> map = new HashMap<>();
                map.put("name", iUser.getFirstName() + " " + iUser.getLastName());
                map.put("code", iUser.getCode());
                List<Map<String, String>> propertyList = propertyMapper.findAllSaleByAgent(iUser.getId());
                map.put("saledProperties", propertyList);
                Map<String, String> map1 = propertyMapper.findAgentSalesTotalCommission(iUser.getId());
                map.put("saledComission", map1);
                mapList.add(map);
            }
        }
        return mapList;
    }


    public List<Map<String, Object>> findAllAgentByReferId(String referid) {
        List<Map<String, Object>> mapList = userMapper.findAgent(referid);
        return mapList;
    }

    public List<Map<String, Object>> findSubUserTreeByReferId(String referid){

        List<Map<String, Object>> childrenMapList = userMapper.findAgent(referid);
        for (Map item:childrenMapList) {
            String childCode = (String)item.get("code");
            item.put("label",childCode + " - " + item.get("name"));
            item.put("expanded", true);
            item.put("value", childCode);
        }


        List<Map<String, Object>> root = new ArrayList<>();
        root.add(new HashMap<String, Object>(){{
            IUser root = userMapper.findAllByCode(referid);
            put("label",referid +" - " + root.getFirstName() + " " +root.getLastName() );
            put("expanded", true);
            put("value", referid);
            put("children",childrenMapList);
        }});
        return root;

    }

    public List<Map<String, Object>> findAllUserTreeByReferId(String referid, boolean isRoot){
        Set uniqueCode = new HashSet<String>();
        uniqueCode.add(referid);
        List<Map<String, Object>> childrenMapList = userMapper.findAgent(referid);
        for (Map item:childrenMapList) {
            String childCode = (String)item.get("code");

            item.put("label",childCode + ":" + item.get("name"));
            item.put("expanded", true);
            item.put("value", childCode);
            if(!uniqueCode.contains(childCode)){
                item.put("children",findAllUserTreeByReferId(childCode,false));
                uniqueCode.add(childCode);
            }

        }

        if(isRoot){
            List<Map<String, Object>> root = new ArrayList<>();
            root.add(new HashMap<String, Object>(){{
                put("label","root:" + referid);
                put("expanded", true);
                put("value", referid);
                put("children",childrenMapList);
            }});
            return root;
        }else{
            return childrenMapList;
        }

    }
}